package com.assignwork.modules.plan;

import com.assignwork.dto.*;
import com.assignwork.entity.DailyPlan;
import com.assignwork.entity.DailyPlanShiftTon;
import com.assignwork.entity.ShipDynamic;
import com.assignwork.entity.ThroughputForecastOre;
import com.assignwork.modules.plan.service.IPlanService;
import com.assignwork.service.*;
import lombok.AllArgsConstructor;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.secure.BladeUser;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.DateUtil;
import org.springblade.core.tool.utils.Func;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.time.LocalDate;
import java.util.List;

@RestController
@AllArgsConstructor
@RequestMapping("plan")
public class PlanController {

    ITrainStatisticsService trainStatisticsService;
    IPlanService planService;
    IDailyPlanService dailyPlanService;
    IAssemblyLineService assemblyLineService;
    IStartedCabinService startedCabinService;
    IDynamicFormService dynamicFormService;
    IShipDynamicService shipDynamicService;
    IThroughputForecastService throughputForecastService;
    IThroughputForecastOreService throughputForecastOreService;
    IThroughputForecastHuadongService throughputForecastHuadongService;
    ITrainLoadLogService trainLoadLogService;
    IDailyPlanShiftTonService dailyPlanShiftTonService;

    @GetMapping("list-daily-plan")
    public R listDailyPlan(BladeUser bladeUser, Query query, DailyPlanDTO search) {
        return R.data(dailyPlanService.listDailyPlan(bladeUser, query, search));
    }

    @PostMapping("save-or-update-daily-plan")
    public R saveOrUpdateDailyPlan(BladeUser bladeUser, @RequestBody DailyPlanDTO inputDTO) {
        return R.data(dailyPlanService.saveOrUpdate(inputDTO));
    }

    @GetMapping("get-plan-total-by-voyage")
    public R getPlanTotalByVoyage(BladeUser bladeUser, DailyPlanDTO dto) {
        return R.data(planService.getPlanTotalByVoyage(bladeUser, dto));
    }

    @GetMapping("list-assembly-line")
    public R listAssemblyLine(BladeUser bladeUser, Query query, AssemblyLineDTO search) {
        return R.data(assemblyLineService.listAssemblyLine(bladeUser, query, search));
    }

    @PostMapping("save-or-update-assembly-line")
    public R saveOrUpdateAssemblyLine(BladeUser bladeUser, @RequestBody AssemblyLineDTO input) {
        return R.data(assemblyLineService.saveOrUpdateAssemblyLineInfo(bladeUser, input));
    }

    @PostMapping("remove-assembly-line")
    public R removeAssemblyLine(BladeUser bladeUser, @RequestBody AssemblyLineDTO input) {
        return R.data(assemblyLineService.removeAssemblyLine(bladeUser, input));
    }

    @PostMapping("remove-plan")
    public R removePlan(BladeUser bladeUser, @RequestParam String id) {
        return R.data(dailyPlanService.removeById(Func.toLong(id)));
    }

    @GetMapping("map-started-cabin-ton")
    public R mapStartedCabinTon(BladeUser bladeUser, String planId) {
        return R.data(startedCabinService.mapStartedCabinTon(bladeUser, Func.toLong(planId)));
    }

    @PostMapping("save-or-update-leadership-comments")
    public R saveOrUpdateLeadershipComments(BladeUser bladeUser, @RequestBody DynamicFormDTO input) {
        return R.data(dynamicFormService.saveOrUpdateLeadershipComments(bladeUser, input));
    }

    @GetMapping("form-data")
    public R formData(BladeUser bladeUser, DynamicFormDTO search) {
        return R.data(dynamicFormService.formData(bladeUser, search));
    }

    @GetMapping("weather-tide")
    public R weatherTide(BladeUser bladeUser, String planDate) {
        return R.data(planService.weatherTide(bladeUser, planDate));
    }

    @PostMapping("copy-from-yesterday")
    public R copyFromYesterday(BladeUser bladeUser, @RequestBody DailyPlanDTO input) {
        return R.data(planService.copyFromYesterday(bladeUser, input));
    }

    @GetMapping("list-day-stat-plan")
    public R listDayStatPlan(Query query, String keyword) {
        return R.data(planService.listDayStatPlan(query, keyword));
    }

    @PostMapping("import-data")
    public R importData(BladeUser bladeUser, @RequestBody ImportDataDTO dto) {
        return R.data(planService.importData(bladeUser, dto));
    }

    @GetMapping("list-ship-dynamic")
    public R listShipDynamic(BladeUser bladeUser, Query query, ImportDataDTO dto) {
        return R.data(shipDynamicService.listShipDynamic(bladeUser, query, dto));
    }

    @PostMapping("save-ship-dynamic")
    public R saveShipDynamic(BladeUser bladeUser, @RequestBody ShipDynamic ett) {
        return R.data(shipDynamicService.save(ett));
    }

    @PostMapping("update-ship-dynamic")
    public R updateShipDynamic(BladeUser bladeUser, @RequestBody List<ShipDynamic> etts) {
        return R.data(shipDynamicService.updateBatchById(etts));
    }

    @PostMapping("remove-ship-dynamic")
    public R removeShipDynamic(BladeUser bladeUser, @RequestParam String id) {
        return R.data(shipDynamicService.removeById(Func.toLong(id)));
    }

    @GetMapping("page-throughput-forecast")
    public R pageThroughputForecast(BladeUser bladeUser, Query query, ThroughputForecastDTO dto) {
        return R.data(throughputForecastService.pageThroughputForecast(bladeUser, Condition.getPage(query), dto));
    }

    @PostMapping("save-or-update-throughput-forecast")
    public R saveOrUpdateThroughputForecast(BladeUser bladeUser, @RequestBody ThroughputForecastDTO dto) {
        return R.data(throughputForecastService.saveOrUpdateDTO(dto));
    }

    @PostMapping("remove-throughput-forecast")
    public R removeThroughputForecast(BladeUser bladeUser, @RequestParam String id) {
        return R.data(throughputForecastService.removeById(Func.toLong(id)));
    }

    @GetMapping("statistics-throughput-forecast")
    public R statisticsThroughputForecast(BladeUser bladeUser, Query query, ThroughputForecastDTO dto) {
        return R.data(throughputForecastService.statisticsThroughputForecast(bladeUser, Condition.getPage(query), dto));
    }
    
    @GetMapping("statistics-throughput-forecast-cargo")
    public R statisticsThroughputForecastCargo(BladeUser bladeUser, String groupId, String year) {
        return R.data(throughputForecastService.statisticsThroughputForecastCargo(bladeUser, Func.toLong(groupId), year));
    }
    
    @GetMapping("list-stf-ore")
    public R listStfOre(BladeUser bladeUser, String year) {
        return R.data(this.throughputForecastOreService.listVO(year));
    }

    @PostMapping("save-or-update-stf-ore")
    public R saveOrUpdateStfOre(BladeUser bladeUser, @RequestBody List<ThroughputForecastOre> etts) {
        return R.data(this.throughputForecastOreService.saveOrUpdateBatch(etts));
    }

    @GetMapping("page-train-load-log")
    public R pageTrainLoadLog(BladeUser bladeUser, Query query, TrainLoadLogDTO dto) {
        return R.data(trainLoadLogService.pageTrainLoadLog(bladeUser, Condition.getPage(query), dto));
    }

    @GetMapping("exportTrainLoadLog")
    public void exportTrainLoadLog(HttpServletResponse response, TrainLoadLogDTO dto) {
        trainLoadLogService.trainLoadingExport(response,dto);
    }
    @PostMapping("save-or-update-train-load-log")
    public R saveOrUpdateTrainLoadLog(BladeUser bladeUser, @RequestBody TrainLoadLogDTO dto) {
        return R.data(trainLoadLogService.saveOrUpdate(dto));
    }

    @PostMapping("remove-train-load-log")
    public R removeTrainLoadLog(BladeUser bladeUser, @RequestParam String id) {
        return R.data(trainLoadLogService.removeById(Func.toLong(id)));
    }

    @GetMapping("get-started-cabin-ton")
    public R getStartedCabinTon(BladeUser bladeUser, DailyPlanDTO dto) {
        return R.data(planService.getStartedCabinTon(bladeUser, dto));
    }

    @GetMapping("/statistics-all-throughput-forecast")
    public R statisticsAllThroughputForecast(BladeUser bladeUser, ThroughputForecastDTO dto) {
        return R.data(throughputForecastService.statisticsAllThroughputForecast(bladeUser, dto));
    }

    @GetMapping("page-throughput-forecast-history")
    public R throughputForecastHistory(BladeUser bladeUser, Query query, ThroughputForecastHuadongDTO dto) {
        return R.data(throughputForecastHuadongService.pageVO(bladeUser, Condition.getPage(query), dto));
    }

    @GetMapping("statistics-throughput-forecast-history")
    public R statisticsThroughputForecastHistory(BladeUser bladeUser, ThroughputForecastHuadongDTO dto) {
        return R.data(throughputForecastHuadongService.statisticsThroughputForecastHistory(bladeUser, dto));
    }

    @PostMapping("submit-daily-plan-shift-ton")
    public R submitDailyPlanShiftTon(@RequestBody DailyPlanShiftTon entity) {
        return R.data(dailyPlanShiftTonService.submitDailyPlanShiftTon(entity));
    }

    @GetMapping("get-daily-plan-shift-ton")
    public R getDailyPlanShiftTon(DailyPlanShiftTonSearchDTO dto) {
        return R.data(dailyPlanShiftTonService.getShiftTon(dto));
    }

    @PostMapping("save-or-update-train-statistics")
    public R saveOrUpdateTrainStatistics(BladeUser bladeUser, @RequestBody TrainStatisticsDTO dto) {
        return R.data(trainStatisticsService.saveOrUpdateTrainStatistics(bladeUser, dto));
    }
    
    @GetMapping("detail-train-statistics")
    public R detailTrainStatistics(String planDate) {
        return R.data(trainStatisticsService.detailTrainStatistics(Func.parseDate(planDate)));
    }

    @GetMapping("detail-real-line-info")
    public R detailRealLineInfo(BladeUser bladeUser, DailyPlanDTO dto) {
        return R.data(planService.detailRealLineInfo(dto.getPlanDate()));
    }
}
